//
//  Helper.cpp
//  leetcode
//
//  Created by Ellie on 2021/1/30.
//  Copyright © 2021 Ellie. All rights reserved.
//

#include <stdio.h>
#include "DataDefine.h"
#include <queue>
#include <unordered_map>
#include <string>
#include <stack>
#include <vector>
#include <iostream>
#include <sstream>

#include "Helper.h"

using namespace std;

ListNode* getMedian(ListNode* left, ListNode* right)
{
    if(left == nullptr)
        return nullptr;

    ListNode* fast = left;
    ListNode* slow = left;
    
    while(fast != right && fast->next != right)
    {
        fast = fast->next->next;
        slow = slow->next;
    }
    return slow;
}

ListNode* buildList(vector<int>& input)
{
    if(input.size() == 0)
        return nullptr;
    vector<int>::iterator iter = input.begin();
    ListNode* head = nullptr;
    ListNode* tail = nullptr;
    for(;iter != input.end();iter ++)
    {
        ListNode* node = new ListNode(*iter);
        if(head == nullptr)
        {
            head = node;
            tail = node;
        }
        else{
            tail->next = node;
            tail = node;
        }
    }
    tail->next = nullptr;
    return head;
}

void buildVector(ListNode* input, vector<int>& result)
{
    if(input == nullptr)
        return;
    while(input != nullptr)
    {
        result.push_back(input->val);
        input = input->next;
    }
}

void printList(string notes, ListNode* head)
{
    if(head == nullptr)
        cout << "Notes:" << notes << ",Empty List" << endl;
    cout << notes << ":";
    while(head != nullptr)
    {
        cout << head->val <<  " ";
        head = head->next;
    }
    cout << endl;
}

void printstdlist(string notes, std::list<int>& input)
{
    cout << notes << ":";
    for(auto& v : input)
    {
        cout << v << " ";
    }
    cout << endl;
}

void myshuffle(vector<int>& input)
{
    srand(unsigned(time(NULL)));
    // C++ 11 http://www.cplusplus.com/reference/algorithm/shuffle/
    random_shuffle(input.begin(), input.end());
}

void Print_Nodes(string notes, deque<TreeNode*>& input)
{
    deque<TreeNode*>::iterator iter = input.begin();
    deque<TreeNode*>::iterator end = input.end();
    
    cout << notes << ":";
    for(;iter != end;iter ++)
    {
        cout << (*iter)->val << " ";
    }
    cout << endl;
}
